package com.twb.system.service.impl;

import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.fileCenter.entity.vo.ExportExcelVo;
import com.twb.fileCenter.util.FileUtil;
import com.twb.system.entity.twbEnclosure.TwbEnclosure;
import com.twb.system.entity.twbEnclosure.dto.ListServerFilesDto;
import com.twb.system.entity.twbEnclosure.dto.SearchTwbEnclosureDto;
import com.twb.system.entity.twbEnclosure.vo.BizUpdateTwbEnclosureVo;
import com.twb.system.entity.twbEnclosure.vo.SaveTwbEnclosureVo;
import com.twb.system.entity.twbEnclosure.vo.SearchTwbEnclosureVo;
import com.twb.system.entity.twbEnclosure.vo.UpdateTwbEnclosureByIdVo;
import com.twb.system.mapper.TwbEnclosureMapper;
import com.twb.system.service.ITwbEnclosureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author DHL
 * @ClassName TwbEnclosureService
 * @Description 附件信息service
 * @date 2020-08-24 10:52:32
 */
@Service("twbEnclosureService")
public class TwbEnclosureService extends BaseService<TwbEnclosureMapper, TwbEnclosure> implements ITwbEnclosureService {

  @Autowired
  TwbEnclosureMapper twbEnclosureMapper;

  @Override
  public TwbPageInfo<SearchTwbEnclosureDto> searchTwbEnclosure(SearchTwbEnclosureVo vo) {
    TwbPageUtil.startPage(vo);
    List<SearchTwbEnclosureDto> list = twbEnclosureMapper.searchTwbEnclosure(vo);
    return TwbPageUtil.of(list);
  }

  @Override
  public List<SearchTwbEnclosureDto> searchTwbEnclosureList(SearchTwbEnclosureVo vo) {
    List<SearchTwbEnclosureDto> list = twbEnclosureMapper.searchTwbEnclosure(vo);
    return list;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public TwbEnclosure saveTwbEnclosure(SaveTwbEnclosureVo vo) {
    TwbEnclosure record = new TwbEnclosure();
    BeanUtils.copyNotNullProperties(vo, record);
    record.setId(this.getUUID());
    this.setCreateInfo(record);
    int result = twbEnclosureMapper.insert(record);

    if (result > 0) {
      return record;
    }
    return null;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int updateTwbEnclosureById(UpdateTwbEnclosureByIdVo vo) {
    TwbEnclosure record = twbEnclosureMapper.selectById(vo.getId());
    if (null == record) {
      throw new ServiceException("数据不存在");
    }
    BeanUtils.copyProperties(vo, record);
    this.setUpdateInfo(record);
    int result = twbEnclosureMapper.updateById(record);
    return result;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int deleteTwbEnclosureById(String id) {
    int result = twbEnclosureMapper.deleteById(id);
    return result;
  }

  @Override
  public int deleteTwbEnclosureByIds(String ids) {
    SearchTwbEnclosureVo searchTwbEnclosureVo = new SearchTwbEnclosureVo();
    searchTwbEnclosureVo.setFileIds(ids);
    List<ListServerFilesDto> serverFiles = this.listServerFiles(searchTwbEnclosureVo);
    if (!serverFiles.isEmpty()) {
      int result = twbEnclosureMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
      if (result > 0) {
        for (ListServerFilesDto listServerFilesDto : serverFiles) {
          FileUtil.delServerFile(listServerFilesDto.getBasePath());
        }
      }
      return result;
    }
    return 0;
  }

  @Override
  public int logicBatchDeleteTwbEnclosureByIds(String ids) {
    List<String> list = new ArrayList<String>();
    Collections.addAll(list, ids.split(","));
    return twbEnclosureMapper.logicBatchDeleteByIds(list);
  }

  @Override
  public void exportExcel(HttpServletRequest request, ExportExcelVo exportExcelVo, SearchTwbEnclosureVo searchVo) {

  }

  @Override
  public List<ListServerFilesDto> listServerFiles(SearchTwbEnclosureVo searchTwbEnclosureVo) {

    List<ListServerFilesDto> listServerFiles = twbEnclosureMapper.listServerFiles(searchTwbEnclosureVo);

    return listServerFiles;
  }

  @Override
  public boolean bizUpdateToEnclosure(BizUpdateTwbEnclosureVo vo) {

    if (StringUtils.isBlank(vo.getIds()) || StringUtils.isBlank(vo.getBizId())) {

      return false;
    }
    // 重新挂接业务附件
    int result = twbEnclosureMapper.bizUpdateToEnclosure(vo);

    if (result < 1) {
      return false;
    }

    return true;
  }


  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean bizUpdateToEnclosure(String bizId, String fileIds, Class c) {
    BizUpdateTwbEnclosureVo bizUpdateTwbEnclosureVo = new BizUpdateTwbEnclosureVo();
    bizUpdateTwbEnclosureVo.setBizId(bizId);
    bizUpdateTwbEnclosureVo.setIds(fileIds);
    bizUpdateTwbEnclosureVo.setClazz(c);
    return this.bizUpdateToEnclosure(bizUpdateTwbEnclosureVo);
  }


  @Override
  public boolean bizAddToEnclosure(BizUpdateTwbEnclosureVo vo) {

    if (StringUtils.isBlank(vo.getIds()) || StringUtils.isBlank(vo.getBizId())) {

      return false;
    }
    // 重新挂接业务附件
    int result = twbEnclosureMapper.bizAddToEnclosure(vo);

    if (result < 1) {
      return false;
    }

    return true;
  }
}
